package com.wemei.auto.ui.widget

import android.app.Activity
import android.content.Context
import android.content.res.TypedArray
import android.text.Editable
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.EditText
import android.widget.FrameLayout
import android.widget.TextView
import com.wemei.auto.ui.R
import com.wemei.auto.ui.databinding.ViewWhiteSearchBinding
import com.wemei.auto.ui.listener.OnSearchEditorActionListener
import com.wemei.auto.ui.listener.TextWatcher

class SearchView : FrameLayout {
    companion object {
        const val SV_WHITE = 1 // 其他位置，白色
    }

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        init(attrs)
    }

    private var mTblStyle: Int = SV_WHITE
    private var showSearchText = false
    private var isShowLeft = false
    private var mViewWhiteSearchBinding: ViewWhiteSearchBinding? = null
    var hint: String = ""
        set(value) {
            mViewWhiteSearchBinding?.searchBar?.hint = value
            field = value
        }
    var onLeftClickListener: ((view: View) -> Unit)? = null
    var clearListener: (() -> Unit)? = null
        set(value) {
            mViewWhiteSearchBinding?.run {
                searchBar.setOnEditorActionListener(OnSearchEditorActionListener { value?.invoke() })
                searchBar.clearListener = value
                searchBar.addTextChangedListener(TextWatcher(searchBar) { _: TextView, editable: Editable ->
                    if (editable.isEmpty()) {
                        value?.invoke()
                    }
                })
                tvSearch.setOnClickListener { value?.invoke() }
            }
            field = value
        }
    val text: String
        get() {
            if (mViewWhiteSearchBinding != null) {
                return mViewWhiteSearchBinding!!.searchBar.text.toString()
            }
            return ""
        }
    val inputEditText: EditText
        get() {
            return mViewWhiteSearchBinding!!.searchBar
        }

    private fun init(attrs: AttributeSet?) {
        attrs?.let {
            val typedArray: TypedArray = context.obtainStyledAttributes(attrs, R.styleable.SearchView)
            mTblStyle = typedArray.getInt(R.styleable.SearchView_sv_style, mTblStyle)
            showSearchText = typedArray.getBoolean(R.styleable.SearchView_sv_show_search_text, showSearchText)
            isShowLeft = typedArray.getBoolean(R.styleable.SearchView_sv_show_left, isShowLeft)
            typedArray.recycle()
        }
        when (mTblStyle) {
            SV_WHITE -> {
                mViewWhiteSearchBinding = ViewWhiteSearchBinding.inflate(LayoutInflater.from(context), this, true)
                mViewWhiteSearchBinding?.tvSearch?.visibility = if (showSearchText) View.VISIBLE else View.GONE
                mViewWhiteSearchBinding?.ivBack?.visibility = if (isShowLeft) View.VISIBLE else View.GONE
                mViewWhiteSearchBinding?.ivBack?.setOnClickListener { (context as? Activity)?.onBackPressed() }
            }
        }
    }
}